const through2 = require('through2');
const gutil = require('gulp-util');
const Transpiler = require('./lib/transpiler');

const PLUGIN_NAME = 'gulp-vue-sfc-transpiler';

module.exports = function (option) {
    return through.obj(function (chunk, encoding, callback) {
        if (chunk.isNull() || !chunk.isBuffer()) {
            return callback(null, chunk);
        }

        if (chunk.isStream()) {
            this.emit('error', new gutil.PluginError(PLUGIN_NAME, '不支持文件流'));
            return callback();
        }

        let content = chunk.contents.toString(encoding);
        let transpiler = new Transpiler(content, option);
        let result = transpiler.transform(chunk.path);
        chunk.contents = new Buffer(result);
        callback(null, chunk);
    });
};
